﻿using CommonLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YouErYuanBLL;
using YouErYuanModels;

namespace YouErYuanManage.Controllers
{
    public class TeacherSettingController : Controller
    {
        [YouErYuanManage.Filter.IsLogin]
        public string GetTeacherList()
        {
            TeacherBLL cbll = new TeacherBLL();
            int pageIndex = 0;
            int pageSize = 0;
            try
            {
                pageIndex = Convert.ToInt32(Request["pageIndex"]);
                pageSize = Convert.ToInt32(Request["pageSize"]);
            }
            catch (Exception)
            {
                pageIndex = 1;
                pageSize = 10;
            }
            string schoolid = (HttpContext.Session["user"] as Teacher).Schoolid;
            int total = 0;
            object dt = cbll.TeacherPageList(pageIndex, pageSize, schoolid, out total);
            int pageCount = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;//总页数
            var data = new
            {
                pageIndex = pageIndex,
                pageCount = pageCount,
                list = dt,
                total = total
            };
            return CommonMethod.FormatJsonObj("", data);
        }

        /// <summary>
        /// 根据id查找
        /// </summary>
        /// <returns></returns>
        public string SelectForId()
        {
            TeacherBLL cbll = new TeacherBLL();
            string id = Request["id"];
            object t = cbll.GetTeacherForId(id);
            //查询选中的班级
            object t2 = cbll.GetTeacherReationList(id);
            var data = new { t, t2 };
            return CommonMethod.FormatJsonObj("", data);
        }

        /// <summary>
        /// 新增或修改
        /// </summary>
        /// <returns></returns>
        [YouErYuanManage.Filter.IsLogin]
        public string SaveTeacher(Teacher model)
        {
            TeacherBLL cbll = new TeacherBLL();

            if (string.IsNullOrEmpty(model.Phone) || string.IsNullOrWhiteSpace(model.Phone))
            {
                return CommonMethod.FormatJsonObj("", "手机号不能为空");
            }
            if (string.IsNullOrEmpty(model.password) || string.IsNullOrWhiteSpace(model.password))
            {
                return CommonMethod.FormatJsonObj("", "密码不能为空");
            }
            //验证手机号不能重复
            if (string.IsNullOrEmpty(model.Id))
            {
                //新增
                int ckphonecount = new TeacherBLL().CheckTeacherPhone(model.Phone);
                if (ckphonecount == 1)
                {
                    return CommonMethod.FormatJsonObj("", "该手机号已存在");
                }
            }
            else
            {
                //修改
                Teacher oldMode = new TeacherBLL().GetTeacherById(model.Id);
                if (oldMode != null)
                {
                    if (!oldMode.Phone.Equals(model.Phone))
                    {
                        //验证唯一
                        int ckphonecount = new TeacherBLL().CheckTeacherPhone(model.Phone);
                        if (ckphonecount == 1)
                        {
                            return CommonMethod.FormatJsonObj("", "该手机号已存在");
                        }
                    }
                }
            }
            model.Schoolid = (HttpContext.Session["user"] as Teacher).Schoolid;
            model.password = CommonLib.DESencodingTxt.DESencrypt(model.password);
            int count = 0;
            if (string.IsNullOrEmpty(model.Id) || string.IsNullOrWhiteSpace(model.Id))
            {
                model.Id = Guid.NewGuid().ToString();
                //新增               
                count = cbll.Add(model);
            }
            else
            {
                //修改
                count = cbll.Update(model);
            }
            return CommonMethod.FormatJsonObj(count);
        }

        [YouErYuanManage.Filter.IsLogin]
        public string DeleteTeacher(string teacherId)
        {
            if (string.IsNullOrEmpty(teacherId) || string.IsNullOrWhiteSpace(teacherId))
            {
                return CommonLib.CommonMethod.FormatJsonObj("", "teacherId不能为空");
            }
            int efcount = new TeacherBLL().DeleteTeacher(teacherId);
            return CommonLib.CommonMethod.FormatJsonObj(efcount);
        }

        /// <summary>
        /// 验证账号是否存在
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public string CheckTeacherPhone(string phone)
        {
            int count = new TeacherBLL().CheckTeacherPhone(phone);
            return CommonMethod.FormatJsonObj(count);
        }

        /// <summary>
        /// 验证管理员账号是否存在
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        //[YouErYuanManage.Filter.IsLogin]
        //public string CheckAdminPhone(string phone)
        //{
        //    Teacher model = Session["user"] as Teacher;
        //    if (model != null)
        //    {
        //        int count = new TeacherBLL().CheckTeacherPhone(phone, model.Type);
        //        return CommonMethod.FormatJsonObj(count);
        //    }
        //    return CommonMethod.FormatJsonObj("");
        //}

        /// <summary>
        /// 保存用户设置
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="pwd"></param>
        /// <param name="name">姓名</param>
        /// <returns></returns>
        [YouErYuanManage.Filter.IsLogin]
        public string SaveUserSetting(string phone, string pwd, string name)
        {
            Teacher teacher = Session["user"] as Teacher;
            if (teacher != null)
            {
                int isExit = 0;
                if (teacher.Phone.Equals(phone) == false)
                {
                    //账号不同，验证账号是否重复
                    //if (teacher.Type.Equals("3"))
                    //{
                    //    isExit = new TeacherBLL().CheckTeacherPhone(phone, "3");
                    //}
                    //else if (teacher.Type.Equals("2"))
                    //{
                    //    isExit = new TeacherBLL().CheckTeacherPhone(phone);
                    //}
                    isExit = new TeacherBLL().CheckTeacherPhone(phone);
                }
                if (isExit > 0)
                {
                    return CommonMethod.FormatJsonObj(11);
                }
                else
                {
                    int cou = new TeacherBLL().UpdateTeacher(phone, pwd, name, teacher.Id);
                    return CommonMethod.FormatJsonObj(cou);
                }
            }
            return CommonMethod.FormatJsonObj(0);
        }

    }
}